#pragma once
#include<assert.h>
#include<iostream>
#include"ReverseIterator.h"

namespace my_list
{
	template<class T>

	struct __list_node
	{
		__list_node<T>* _next;
		__list_node<T>* _prev;
		T _val;

		__list_node(const T& val = T())
			:_next(nullptr)
			,_prev(nullptr)
			,_val(val)
		{}


	};


	template<class T,class Ref,class Ptr>
	struct __list_iterator
	{
		typedef __list_node<T> Node;
		typedef __list_iterator<T, Ref, Ptr> Self;

		Node* _pos;

		__list_iterator(Node* pos)
			:_pos(pos)
		{}

		Ref operator*()
		{
			return _pos->_val;
		}

		Ptr operator->()
		{
			return &_pos->_val;
		}

		Self& operator++()
		{
			_pos = _pos->_next;
			return *this;
		}

		Self& operator--()
		{
			_pos =  _pos->_prev;
			return *this;
		}

		Self operator++(int)
		{
			Self tmp(*this);
			_pos = _pos->_next;
			return tmp;
		}

		Self operator--(int)
		{
			Self tmp(*this);
			_pos =  _pos->_prev;
			return tmp;
		}

		bool operator!=(const Self& self)
		{
			return _pos != self._pos;
		}
		bool operator==(const Self& self)
		{
			return _pos == self._pos;
		}
	};


	template<class T>
	class list
	{
		typedef __list_node<T> Node;
	public:
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T, const T&, const T*> const_iterator;
		typedef my_reverse_iterator::Reverse_Iterator<iterator, T&, T*> reverse_iterator;
		typedef my_reverse_iterator::Reverse_Iterator<const_iterator, const T&, const T*> const_reverse_iterator;

		reverse_iterator rbegin()
		{
			return end();
		}

		reverse_iterator rend()
		{
			return begin();
		}

		const_reverse_iterator rbegin()const
		{
			return end();
		}

		const_reverse_iterator rend()const
		{
			return begin();
		}

		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator begin()const
		{
			return const_iterator(_head->_next);
		}

		const_iterator end()const
		{
			return const_iterator(_head);
		}

		list()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}

		

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
			
		}

		list(const list<T>& lt)
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;

			for (auto& e : lt)
			{
				push_back(e);
			}
		}

		void Swap(list<T>& t)
		{
			std::swap(_head, t._head);
			std::swap(_size, t._size);
		}

		list<T>& operator=(list<T> lt)
		{
			Swap(lt);

			return *this;
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
			_size = 0;
		}


		iterator insert(iterator pos, const T& val)
		{
			
			Node* cur = pos._pos;
			Node* prev = cur->_prev;
			Node* newnode = new Node(val);

			newnode->_next = cur;
			cur->_prev = newnode;
			prev->_next = newnode;
			newnode->_prev = prev;
			_size++;

			return newnode;
		}


		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* del = pos._pos;
			Node* next = del->_next;
			Node* prev = del->_prev;

			prev->_next = next;
			next->_prev = prev;

			delete del;
			_size--;

			return next;
		}


		

		void push_back(const T& x)
		{
			insert(end(), x);
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		void pop_back()
		{
			erase(_head->_prev);
		}

		void pop_front()
		{
			erase(_head->_next);
		}

	private:
		Node* _head;
		size_t _size;
	};
}